
var prefix = "/camera"
$(function() {
	load();
});

function load() {
	$('#cameraTable')
			.bootstrapTable(
					{
						method : 'get', // 服务器数据的请求方式 get or post
						url : prefix + "/list", // 服务器数据的加载地址
						iconSize : 'outline',
						toolbar : '#exampleToolbar',
						striped : true, // 设置为true会有隔行变色效果
						dataType : "json", // 服务器返回的数据类型
						pagination : true, // 设置为true会在底部显示分页条
						singleSelect : false, // 设置为true将禁止多选
						pageSize : 10, // 如果设置了分页，每页数据条数
						pageNumber : 1, // 如果设置了分布，首页页码
						showColumns : false, // 是否显示内容下拉框（选择显示的列）
						sidePagination : "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
						queryParams : function(params) {
							var brand = $("#brand_slt").selectpicker('val');
                            var direction = $("#direction_slt").selectpicker('val');
                            var cameraIp = $("#searchIp").val();
                            var communityId = $("#communityId").val();
                            var cameraName = $("#searchName").val();
                            var status = $("#status_slt").selectpicker('val');
							return {
								limit: params.limit,
								offset:params.offset,
                                brand:brand,
                                direction:direction,
                                cameraIp:cameraIp,
                                communityId:communityId,
                                cameraName:cameraName,
							    status:status
							};
						},
						columns : [
                            {
                                field : 'id',
                                title : '相机ID'
                            },{
                                field : 'cameraName',
                                title : '相机名称'
                            },
                                  {
									field : 'status',
									title : '状态',
                                    align : 'center',
									formatter : function(value, row, index) {
										if (value == '1') {
											return '<span class="label label-primary">在线</span>';
										} else if (value == '0') {
                                            return '<span class="label label-danger">断开</span>';
                                        }else if (value == '2') {
                                            return '<span class="label label-danger">故障</span>';
                                        }
									}
								},
								{
									field : 'direction',
									title : '方向',
									align : 'center',
                                    formatter : function(value, row, index) {
                                        if (value == '1') {
                                            return '<span class="label label-primary">入口</span>';
                                        } else if (value == '2') {
                                            return '<span class="label label-info">出口</span>';
                                        }说
                                    }
								},
								{
									field : 'brand',
									title : '品牌',
									align : 'center',
                                    formatter : function(value, row, index) {
                                        if (value == '1') {
                                            return '<span class="label label-primary">华夏</span>';
                                        } else if (value == '2') {
                                            return '<span class="label label-info">中实</span>';
                                        }说
                                    }
								},
								{
									field : 'model',
									title : '型号',
									align : 'center'
								},
								{
									field : 'location',
									title : '位置',
									align : 'center'
								},
								{
									field : 'cameraIp',
									title : '相机IP',
                                    align : 'center',
									width:'100'
								},
								{
									field : 'communityName',
									title : '所属小区',
                                    width:'150'
								},
                            {
                                field : 'syncStatus',
                                title : '同步状态',
                                formatter : function(item, index) {
                                    if (item == '0') {
                                        return '<span class="label label-warning">未同步</span>';
                                    } else if (item == '1') {
                                        return '<span class="label label-info">同步中</span>';
                                    } else if (item == '2') {
                                        return '<span class="label label-success">同步成功</span>';
                                    } else if (item == '3') {
                                        return '<span class="label label-danger">同步失败</span>';
                                    }
                                }
                            },
                            {
                                title : '操作',
                                width : '250',
                                field : 'id',
                                align : 'center',
                                formatter : function(value, row, index) {
                                    var e = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                        + row.id
                                        + '\')"><i class="fa fa-edit"></i></a> ';
                                    var d = '<a class="btn btn-warning btn-sm" href="#" title="删除"  mce_href="#" onclick="remove('
                                        + row.id + "," + row.communityId
                                        + ')"><i class="fa fa-remove"></i></a> ';

                                    var s = '<a class="btn btn-info btn-sm" href="#" title="同步"  mce_href="#" onclick="sync('
                                        + row.id + ',' + row.communityId
                                        + ')"><i class="fa fa-cloud-download"></i></a> ';

                                    var f = '<a class="btn btn-info btn-sm" href="#" title="开闸"  mce_href="#" onclick="openGate(\''
                                        + index
                                        + '\')"><i class="fa fa-arrows-h"></i></a> ';

                                    var g = '<a class="btn btn-info btn-sm" href="#" title="支付码"  mce_href="#" onclick="qrcode(\''
                                        + row.id
                                        + '\')"><i class="fa fa-qrcode"></i></a> ';

                                    var v = '<a class="btn btn-info btn-sm" href="#" title="视频流"  mce_href="#" onclick="vedio(\''
                                        + row.id
                                        + '\')"><i class="fa fa-cc"></i></a> ';

                                    return e + d + s + f + g +v;
                                }
                            } ]
					});
}
function reLoad() {
	$('#cameraTable').bootstrapTable('refresh');
}
function add() {
	layer.open({
		type : 2,
		title : '新增相机',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : prefix + '/add' // iframe的url
	});
}

function checkPlugin(){
    var flag = false;
    if(window.ActiveXObject){
        try{
            var swf = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            if(swf){
                flag = true;
            }
        }catch(e){
        }
    }else{
        try{
            var swf = navigator.plugins['Shockwave Flash'];
            if(swf){
                flag = true;
            }
        }catch(e){
        }
    }
    return flag;
}

function vedio(id) {
    var vediopath = "vedio/" + id;
    var height = "560";
    var title = "查看相机视频";
    var maxmin = true;
    var offset = "10px"
    if(!checkPlugin()){
        vediopath = "plugin";
        height = "220";
        title = "请安装插件";
        maxmin = false;
        offset = "80px"
    }
    layer.open({
        type : 2,
        title : title,
        maxmin : maxmin,
        offset:offset,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '800px',height+ 'px' ],
        content : prefix + '/'+vediopath // iframe的url
    });
}

function edit(id) {
    layer.open({
        type : 2,
        title : '编辑相机',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '800px', '520px' ],
        content : prefix + '/edit/' + id // iframe的url
    });

}

function sync(cameraId) {
    $.ajax({
        url : prefix+"/sync",
        type : "post",
        data : {
            "cameraId" : cameraId
        },
        success : function(r) {
            if (r.code==0) {
                layer.msg(r.msg);
                reLoad();
            }else{
                layer.msg(r.msg);
            }
        }
    });
}
function qrcode(id) {
    layer.open({
        type : 2,
        title : '支付二维码',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '600px', '600px' ],
        content : prefix + '/qrcode/' + id // iframe的url
    });

}


function remove(id, communityId) {

    layer.confirm('确定要删除选中的记录？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajax({
            cache: false,
            type: "POST",
            url: "/camera/delCamera",
            data: {
                "id": id,
                "communityId": communityId
            },
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg("操作成功");
                    reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    layer.close(index);

                } else {
                    layer.alert(data.msg)
                }

            }
        });
    });
}

function choose() {
    layer.open({
        type : 2,
        title : '选择小区',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '800px', '480px' ],
        content : '/car/black/choose' // iframe的url
    });
}

function setComunityInfo(id,name){
    $("#communityId").val(id);
    $("#communityName").val(name);
}
//打开闸机
function openGate(index){
    var tableData = $('#cameraTable').bootstrapTable('getData');
    var cameraName = tableData[index].cameraName;
    var params ={
        'cameraId' : tableData[index].id,
        'communityId':tableData[index].communityId
    };

    layer.confirm('确定要打开【'+cameraName+'】处闸机吗？', {
        btn : [ '确定', '取消' ]
    }, function() {
        $.ajaxSetup({
            contentType:"application/json"
        });
        $.ajax({
            url : prefix+"/openGate",
            type : "post",
            data : JSON.stringify(params),
            success : function(r) {
                if (r.code==0) {
                    layer.msg(r.msg);
                    reLoad();
                }else{
                    layer.msg(r.msg);
                }
            }
        });
    })
}

function clearLoad(){
    $("#searchName").val('');
    $("#searchIp").val('');
    $("#communityId").val('');
    $("#communityName").val('');
    $("#brand_slt").selectpicker('val', '');
    $("#status_slt").selectpicker('val', '');
    $("#direction_slt").selectpicker('val', '');
    $('#cameraTable').bootstrapTable('refresh');
}